﻿using Manon.Core.Extensions;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace Manon.Core.Commons
{
    public class ApplicationUser : IApplicationUser
    {
        public ApplicationUser(IHttpContextAccessor httpContextAccessor)
        {
            if (httpContextAccessor != null && httpContextAccessor.HttpContext != null)
            {
                ClaimsPrincipal claimsPrincipal = httpContextAccessor.HttpContext.User;

                IEnumerable<Claim> claims = claimsPrincipal.Claims;
                if (claims.Any())
                {
                    foreach (var claim in claims)
                    {
                        // 1、获取用户Id
                        if (claim.Type.Equals("UserId", StringComparison.CurrentCultureIgnoreCase))
                        {
                            this.UserId = Convert.ToInt32(claim.Value);
                        }
                        // 2、获取用户名
                        if (claim.Type.Equals("UserName", StringComparison.CurrentCultureIgnoreCase))
                        {
                            this.UserName = claim.Value;
                        }
                        // 2、获取用户名
                        if (claim.Type.Equals("RealName", StringComparison.CurrentCultureIgnoreCase))
                        {
                            this.RealName = claim.Value;
                        }

                        if (claim.Type.Equals("UserType", StringComparison.CurrentCultureIgnoreCase))
                        {
                            SetUserType(int.Parse(claim.Value));
                        }
                        if (claim.Type.Equals("UserInfo", StringComparison.CurrentCultureIgnoreCase) && !string.IsNullOrEmpty(claim.Value))
                        {
                            this.UserInfo = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(claim.Value);
                        }
                    }


                    return;
                }
                var _context = httpContextAccessor.HttpContext;

                string userId = _context.Request.Headers["UserId"];
                if (!string.IsNullOrEmpty(userId))
                {
                    this.UserId = Convert.ToInt32(userId);
                }

                string userInfo = _context.Request.Headers["UserInfo"];
                this.UserName = _context.Request.Headers["UserName"];
                this.RealName = _context.Request.Headers["RealName"];
                if (!string.IsNullOrEmpty(this.RealName))
                {
                    this.RealName = Encoding.UTF8.GetString(Convert.FromBase64String(this.RealName));
                }
                string _userType = _context.Request.Headers["UserType"];
                if (!string.IsNullOrEmpty(_userType))
                {
                    SetUserType(int.Parse(_userType));
                }

                if (userInfo != null && !string.IsNullOrEmpty(userInfo))
                {
                    this.UserInfo = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(Encoding.UTF8.GetString(Convert.FromBase64String(userInfo)));
                }

            }
        }

        private void SetUserType(int userType)
        {
            if (userType == (int)UserTypeEnum.Admin)
            {
                this.IsAdmin = true;
                this.IsWarehouseManager = false;
                this.IsDeliver = false;
                this.IsDriver = false;
                this.IsStationMaster = false;
            }
            else if (userType == (int)UserTypeEnum.Driver)
            {
                this.IsAdmin = false;
                this.IsWarehouseManager = false;
                this.IsDeliver = false;
                this.IsDriver = true;
                this.IsStationMaster = false;
            }
            else if (userType == (int)UserTypeEnum.Deliver)
            {
                this.IsAdmin = false;
                this.IsWarehouseManager = false;
                this.IsDeliver = true;
                this.IsDriver = false;
                this.IsStationMaster = false;
            }
            else if (userType == (int)UserTypeEnum.WarehouseManager)
            {
                this.IsAdmin = false;
                this.IsWarehouseManager = true;
                this.IsDeliver = false;
                this.IsDriver = false;
                this.IsStationMaster = false;
            }
            else if (userType == (int)UserTypeEnum.StationMaster)
            {
                this.IsAdmin = false;
                this.IsWarehouseManager = false;
                this.IsDeliver = false;
                this.IsDriver = false;
                this.IsStationMaster = true;
            }
        }

        /// <summary>
        /// 用户id
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// 租户id
        /// </summary>
        public int TenantId { get; set; }


        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 真实姓名
        /// </summary>
        public string RealName { get; set; }

        /// <summary>
        /// 用户其他信息
        /// </summary>
        public Dictionary<string, string>? UserInfo { get; set; }
        /// <summary>
        /// 是否平台管理员
        /// </summary>
        public bool IsAdmin { get; set; }

        /// <summary>
        /// 是否是仓管
        /// </summary>
        public bool IsWarehouseManager { get; set; }

        /// <summary>
        /// 是否是司机
        /// </summary>
        public bool IsDriver { get; set; }

        /// <summary>
        /// 是否是站长
        /// </summary>
        public bool IsStationMaster { get; set; }

        /// <summary>
        /// 是否是配送员
        /// </summary>
        public bool IsDeliver { get; set; }

        /// <summary>
        /// 是否已经登陆
        /// </summary>
        public bool IsLogin
        {
            get
            {
                return this.UserId > 0;
            }
            set { }
        }


        public string Get(string key)
        {
            if (UserInfo == null || UserInfo.Any() == false || !UserInfo.ContainsKey(key))
            {
                return string.Empty;
            }

            return UserInfo[key];
        }
    }
}
